﻿#ifndef __SS_REDISSQL_H__
#define __SS_REDISSQL_H__
#include "mysql/primary/ssMySqlPy.h"
#include "redis/ssRedisConnection.h"
#include "container/ssAny.h"
#include <vector>


#define REDIS_SQL_REDIS 1
#define REDIS_SQL_MYSQL 2

namespace StarSeeker
{

class ssRedisSql
{
public:
	ssRedisSql();
	~ssRedisSql();
	//设置连接
	void Init(const char* mysqlhost, const char* mysqlacc, const char* mysqlpasswd, const char* mysqldb, unsigned int mysqlport, const char *redishost, int redisport);
	//设置表名
	ssRedisSql& Tables(const char* tables);
	//设置主键：字段名，字段值
	ssRedisSql& Keys(std::initializer_list<ssAny>&& keys);
	//设置字段名
	ssRedisSql& Fields(std::initializer_list<std::string>&& fields);
	//设置字值
	ssRedisSql& Values(std::initializer_list<ssAny>&& values);
	//查询
	void Select(std::vector<std::string>& out, char type=3);
	//插入
	void Insert(char type = 3);
	//更新
	void Update(char type = 3);
	//删除
	void Delete(char type = 3);
private:
	void ClearStream();
	void WriteAny(ssAny& val, bool quotes);
	static std::string ArrayToString(std::vector<std::string>& arr ,char separator=',', char surround=0);
private:
	std::vector<std::string>	m_fields;//字段
	std::vector<ssAny>			m_values;//字段值
	std::string					m_tables;//表
	std::vector<ssAny>			m_keys;//主键
	ssMySqlPy					m_mysql;
	ssRedisConnection			m_redis;
	std::string					m_sql;
	std::stringstream			m_stream;
};

#define  SS_REDISSQL_FORMAT_BEGIN	ClearStream();
#define  SS_REDISSQL_FORMAT_END	m_sql = m_stream.str();

}
#endif
